﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;
namespace SapB1WebPOS.Utils
{
    public class DocumentXML
    {
        public static string ToXMLStringFromDS(String ObjType, DataSet ds)
        {
            try
            {
                StringBuilder XmlString = new StringBuilder();
                XmlWriter writer = XmlWriter.Create(XmlString);
                writer.WriteStartDocument();
                {
                    writer.WriteStartElement("BOM");
                    {
                        writer.WriteStartElement("BO");
                        {
                            #region write ADMINFO_ELEMENT
                            writer.WriteStartElement("AdmInfo");
                            {
                                writer.WriteStartElement("Object");
                                {
                                    writer.WriteString(ObjType);
                                }
                                writer.WriteEndElement();
                            }
                            writer.WriteEndElement();
                            #endregion

                            #region Header&Line XML
                            foreach (DataTable dt in ds.Tables)
                            {
                                writer.WriteStartElement(dt.TableName.ToString());
                                {
                                    foreach (DataRow row in dt.Rows)
                                    {
                                        writer.WriteStartElement("row");
                                        {
                                            foreach (DataColumn column in dt.Columns)
                                            {
                                                if (column.DefaultValue.ToString() != "xx_remove_xx")
                                                {
                                                    if (column.ColumnName != "No")
                                                    {
                                                        writer.WriteStartElement(column.ColumnName); //Write Tag
                                                        {
                                                            writer.WriteString(row[column].ToString());
                                                        }
                                                        writer.WriteEndElement();
                                                    }
                                                }
                                            }
                                        }
                                        writer.WriteEndElement();
                                    }
                                }
                                writer.WriteEndElement();
                            }
                            #endregion
                        }
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndDocument();

                writer.Flush();

                return XmlString.ToString();
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

        public static string UpdateXMLString(String XMLString, string TableName, string ColumnName, string value)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(XMLString);

            XmlNodeList xnList = xml.SelectNodes("/BOM/BO/" + TableName + "/row/" + ColumnName);
            foreach (XmlNode xn in xnList)
            {
                if (xn.Name == ColumnName)
                {
                    xn.InnerText = value;
                    break;
                }
            }
            StringWriter sw = new StringWriter();
            XmlTextWriter xw = new XmlTextWriter(sw);
            xml.WriteTo(xw);
            return sw.ToString();
        }
    }
}